package cn.iocoder.yudao.module.worker.controller.admin.clerkfans;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.admin.clerkfans.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerkfans.ClerkFansDO;
import cn.iocoder.yudao.module.worker.service.clerkfans.ClerkFansService;

@Tag(name = "管理后台 - 店员关注")
@RestController
@RequestMapping("/worker/clerk-fans")
@Validated
public class ClerkFansController {

    @Resource
    private ClerkFansService clerkFansService;

    @PostMapping("/create")
    @Operation(summary = "创建店员关注")
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:create')")
    public CommonResult<Long> createClerkFans(@Valid @RequestBody ClerkFansSaveReqVO createReqVO) {
        return success(clerkFansService.createClerkFans(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员关注")
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:update')")
    public CommonResult<Boolean> updateClerkFans(@Valid @RequestBody ClerkFansSaveReqVO updateReqVO) {
        clerkFansService.updateClerkFans(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员关注")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:delete')")
    public CommonResult<Boolean> deleteClerkFans(@RequestParam("id") Long id) {
        clerkFansService.deleteClerkFans(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:query')")
    public CommonResult<ClerkFansRespVO> getClerkFans(@RequestParam("id") Long id) {
        ClerkFansDO clerkFans = clerkFansService.getClerkFans(id);
        return success(BeanUtils.toBean(clerkFans, ClerkFansRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员关注分页")
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:query')")
    public CommonResult<PageResult<ClerkFansRespVO>> getClerkFansPage(@Valid ClerkFansPageReqVO pageReqVO) {
        PageResult<ClerkFansDO> pageResult = clerkFansService.getClerkFansPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClerkFansRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员关注 Excel")
    @PreAuthorize("@ss.hasPermission('worker:clerk-fans:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkFansExcel(@Valid ClerkFansPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkFansDO> list = clerkFansService.getClerkFansPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员关注.xls", "数据", ClerkFansRespVO.class,
                        BeanUtils.toBean(list, ClerkFansRespVO.class));
    }

}